<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="0:普通用户，1:实名用户，保留为以后的用户与认证" prop="role">
      <el-input v-model="dataForm.role" placeholder="0:普通用户，1:实名用户，保留为以后的用户与认证"></el-input>
    </el-form-item>
    <el-form-item label="建议6-18位账号,用手机或邮箱" prop="username">
      <el-input v-model="dataForm.username" placeholder="建议6-18位账号,用手机或邮箱"></el-input>
    </el-form-item>
    <el-form-item label="建议6-16位密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="建议6-16位密码"></el-input>
    </el-form-item>
    <el-form-item label="每个用户的昵称唯一" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="每个用户的昵称唯一"></el-input>
    </el-form-item>
    <el-form-item label="帐号启用状态：0封禁、1启用" prop="status">
      <el-input v-model="dataForm.status" placeholder="帐号启用状态：0封禁、1启用"></el-input>
    </el-form-item>
    <el-form-item label="积分" prop="integration">
      <el-input v-model="dataForm.integration" placeholder="积分"></el-input>
    </el-form-item>
    <el-form-item label="成长值" prop="growth">
      <el-input v-model="dataForm.growth" placeholder="成长值"></el-input>
    </el-form-item>
    <el-form-item label="职业" prop="job">
      <el-input v-model="dataForm.job" placeholder="职业"></el-input>
    </el-form-item>
    <el-form-item label="个性签名" prop="personalSign">
      <el-input v-model="dataForm.personalSign" placeholder="个性签名"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="性别：0->未知；1->男；2->女" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别：0->未知；1->男；2->女"></el-input>
    </el-form-item>
    <el-form-item label="" prop="age">
      <el-input v-model="dataForm.age" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="可以不用，先备上，-1:未知，0:小学，1:初中，2:高中，3:大专，4:本科，5:硕士,6:博士及以上" prop="education">
      <el-input v-model="dataForm.education" placeholder="可以不用，先备上，-1:未知，0:小学，1:初中，2:高中，3:大专，4:本科，5:硕士,6:博士及以上"></el-input>
    </el-form-item>
    <el-form-item label="所在城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="所在城市"></el-input>
    </el-form-item>
    <el-form-item label="" prop="registerTime">
      <el-input v-model="dataForm.registerTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="可以不用：访问令牌" prop="accessToken">
      <el-input v-model="dataForm.accessToken" placeholder="可以不用：访问令牌"></el-input>
    </el-form-item>
    <el-form-item label="可以不用：访问令牌时间" prop="expiresIn">
      <el-input v-model="dataForm.expiresIn" placeholder="可以不用：访问令牌时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          role: '',
          username: '',
          password: '',
          nickname: '',
          status: '',
          integration: '',
          growth: '',
          job: '',
          personalSign: '',
          avatar: '',
          gender: '',
          age: '',
          education: '',
          city: '',
          registerTime: '',
          accessToken: '',
          expiresIn: ''
        },
        dataRule: {
          role: [
            { required: true, message: '0:普通用户，1:实名用户，保留为以后的用户与认证不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '建议6-18位账号,用手机或邮箱不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '建议6-16位密码不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '每个用户的昵称唯一不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '帐号启用状态：0封禁、1启用不能为空', trigger: 'blur' }
          ],
          integration: [
            { required: true, message: '积分不能为空', trigger: 'blur' }
          ],
          growth: [
            { required: true, message: '成长值不能为空', trigger: 'blur' }
          ],
          job: [
            { required: true, message: '职业不能为空', trigger: 'blur' }
          ],
          personalSign: [
            { required: true, message: '个性签名不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别：0->未知；1->男；2->女不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          education: [
            { required: true, message: '可以不用，先备上，-1:未知，0:小学，1:初中，2:高中，3:大专，4:本科，5:硕士,6:博士及以上不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '所在城市不能为空', trigger: 'blur' }
          ],
          registerTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          accessToken: [
            { required: true, message: '可以不用：访问令牌不能为空', trigger: 'blur' }
          ],
          expiresIn: [
            { required: true, message: '可以不用：访问令牌时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/common/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.role = data.user.role
                this.dataForm.username = data.user.username
                this.dataForm.password = data.user.password
                this.dataForm.nickname = data.user.nickname
                this.dataForm.status = data.user.status
                this.dataForm.integration = data.user.integration
                this.dataForm.growth = data.user.growth
                this.dataForm.job = data.user.job
                this.dataForm.personalSign = data.user.personalSign
                this.dataForm.avatar = data.user.avatar
                this.dataForm.gender = data.user.gender
                this.dataForm.age = data.user.age
                this.dataForm.education = data.user.education
                this.dataForm.city = data.user.city
                this.dataForm.registerTime = data.user.registerTime
                this.dataForm.accessToken = data.user.accessToken
                this.dataForm.expiresIn = data.user.expiresIn
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/common/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'role': this.dataForm.role,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'nickname': this.dataForm.nickname,
                'status': this.dataForm.status,
                'integration': this.dataForm.integration,
                'growth': this.dataForm.growth,
                'job': this.dataForm.job,
                'personalSign': this.dataForm.personalSign,
                'avatar': this.dataForm.avatar,
                'gender': this.dataForm.gender,
                'age': this.dataForm.age,
                'education': this.dataForm.education,
                'city': this.dataForm.city,
                'registerTime': this.dataForm.registerTime,
                'accessToken': this.dataForm.accessToken,
                'expiresIn': this.dataForm.expiresIn
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
